/**
 * Copyright &copy; 2015-2020 <a href="http://www.jeeplus.org/">JeePlus</a> All rights reserved.
 */
package com.jeeplus.modules.tms.web;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.text.SimpleDateFormat;
import java.util.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.ConstraintViolationException;

import com.jeeplus.common.sortList.SortList;
import com.jeeplus.common.utils.excel.ObjectExcelView;
import com.jeeplus.common.utils.excel.PageData;
import com.jeeplus.modules.tms.entity.Resource;
import com.jeeplus.modules.tms.importEntity.StoresImport;
import com.jeeplus.modules.tms.service.ResourceService;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.google.common.collect.Lists;
import com.jeeplus.common.utils.DateUtils;
import com.jeeplus.common.config.Global;
import com.jeeplus.common.json.AjaxJson;
import com.jeeplus.core.persistence.Page;
import com.jeeplus.core.web.BaseController;
import com.jeeplus.common.utils.StringUtils;
import com.jeeplus.common.utils.excel.ExportExcel;
import com.jeeplus.common.utils.excel.ImportExcel;
import com.jeeplus.modules.tms.entity.Stores;
import com.jeeplus.modules.tms.service.StoresService;

/**
 * 仓库Controller
 *
 * @author dhw
 * @version 2018-01-16
 */
@Controller
@RequestMapping(value = "${adminPath}/tms/stores")
public class StoresController extends BaseController {

    @Autowired
    private StoresService storesService;
    @Autowired
    private ResourceService resourceService;

    @ModelAttribute
    public Stores get(@RequestParam(required = false) String id) {
        Stores entity = null;
        if (StringUtils.isNotBlank(id)) {
            entity = storesService.get(id);
        }
        if (entity == null) {
            entity = new Stores();
        }
        return entity;
    }

    /**
     * 仓库列表页面
     */
    @RequiresPermissions("tms:stores:list")
    @RequestMapping(value = {"list", ""})
    public String list() {
        List<Stores> storesList = storesService.findList(new Stores());
        for (Stores stores:storesList) {
            Stores resource1 = new Stores();
            resource1.setResource(stores.getResource());
            List<Stores> storesLists = storesService.findList(resource1);
            if((storesLists.size())==1){
                stores.setIsrepeat("0");
            }else{
                stores.setIsrepeat("1");

            }
            storesService.save(stores);
        }
        return "modules/tms/storesList";
    }

    /**
     * 仓库列表数据
     */
    @ResponseBody
    @RequiresPermissions("tms:stores:list")
    @RequestMapping(value = "data")
    public Map<String, Object> data(Stores stores, HttpServletRequest request, HttpServletResponse response, Model model) {
        Page<Stores> page = storesService.findPage(new Page<Stores>(request, response), stores);
        return getBootstrapData(page);
    }

    /**
     * 查看，增加，编辑仓库表单页面
     */
    @RequiresPermissions(value = {"tms:stores:view", "tms:stores:add", "tms:stores:edit"}, logical = Logical.OR)
    @RequestMapping(value = "form")
    public String form(Stores stores, Model model) {
        model.addAttribute("stores", stores);
        return "modules/tms/storesForm";
    }

    /**
     * 保存仓库
     */
    @ResponseBody
    @RequiresPermissions(value = {"tms:stores:add", "tms:stores:edit"}, logical = Logical.OR)
    @RequestMapping(value = "save")
    public AjaxJson save(Stores stores, Model model, RedirectAttributes redirectAttributes) throws Exception {
        AjaxJson j = new AjaxJson();
        if (!beanValidator(model, stores)) {
            j.setSuccess(false);
            j.setMsg("非法参数！");
            return j;
        }
        List<Stores> storesList = storesService.findList(stores);
        if (storesList.size() == 0) {
            storesService.save(stores);//保存
            j.setSuccess(true);
            j.setMsg("保存仓库成功");
            return j;
        } else {
            int i = 1;
            for (Stores store : storesList) {
                if ((store.getName()).equals((stores.getName())) && (store.getResource()).equals((stores.getResource()))) {
                    i++;
                }
            }

            if (!stores.getIsNewRecord()) {// 编辑表单保存
                int k = 1;
                for (Stores store : storesList) {
                    if ((store.getName()).equals((stores.getName())) && (store.getResource()).equals((stores.getResource())) && !(store.getId()).equals((stores.getId()))) {
                        k++;
                    }
                }
                if (k == 1) {
                    storesService.save(stores);//新建或者编辑保存
                    j.setSuccess(true);
                    j.setMsg("保存仓库成功");
                } else {
                    j.setSuccess(false);
                    j.setMsg("仓库已存在!");
                }
            } else {// 新增表单保存
                if (i == 1) {
                    storesService.save(stores);//新建或者编辑保存
                    j.setSuccess(true);
                    j.setMsg("保存仓库成功");
                } else {
                    j.setSuccess(false);
                    j.setMsg("仓库已存在!");
                }
            }
        }
        return j;
    }

    /**
     * 删除仓库
     */
    @ResponseBody
    @RequiresPermissions("tms:stores:del")
    @RequestMapping(value = "delete")
    public AjaxJson delete(Stores stores, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        storesService.delete(stores);
        j.setMsg("删除仓库成功");
        return j;
    }

    /**
     * 批量删除仓库
     */
    @ResponseBody
    @RequiresPermissions("tms:stores:del")
    @RequestMapping(value = "deleteAll")
    public AjaxJson deleteAll(String ids, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        String idArray[] = ids.split(",");
        for (String id : idArray) {
            storesService.delete(storesService.get(id));
        }
        j.setMsg("删除仓库成功");
        return j;
    }

    /**
     * 导出excel文件
     */
    @ResponseBody
    @RequiresPermissions("tms:stores:export")
    @RequestMapping(value = "export", method = RequestMethod.POST)
    public AjaxJson exportFile(Stores stores, HttpServletRequest request, HttpServletResponse response, RedirectAttributes redirectAttributes) {
        AjaxJson j = new AjaxJson();
        try {
            String fileName = "仓库" + DateUtils.getDate("yyyyMMddHHmmss") + ".xlsx";
            Page<Stores> page = storesService.findPage(new Page<Stores>(request, response, -1), stores);
            new ExportExcel("仓库", Stores.class).setDataList(page.getList()).write(response, fileName).dispose();
            j.setSuccess(true);
            j.setMsg("导出成功！");
            return j;
        } catch (Exception e) {
            j.setSuccess(false);
            j.setMsg("导出仓库记录失败！失败信息：" + e.getMessage());
        }
        return j;
    }

    /**
     * 导入Excel数据
     */
    @RequiresPermissions("tms:stores:import")
    @RequestMapping(value = "imports", method = RequestMethod.POST)
    public String importFiles(MultipartFile file, RedirectAttributes redirectAttributes) {
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<Stores> list = ei.getDataList(Stores.class);
            for (Stores stores : list) {
                try {
                    storesService.save(stores);
                    successNum++;
                } catch (ConstraintViolationException ex) {
                    failureNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条仓库记录。");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条仓库记录" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入仓库失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/stores/?repage";
    }

    /**
     * 下载导入仓库数据模板
     */
    @RequiresPermissions("tms:stores:import")
    @RequestMapping(value = "import/template")
    public String importFileTemplate(HttpServletResponse response, RedirectAttributes redirectAttributes) {
        try {
            String fileName = "仓库数据导入模板.xlsx";
            List<Stores> list = Lists.newArrayList();
            new ExportExcel("仓库数据", Stores.class, 1).setDataList(list).write(response, fileName).dispose();
            return null;
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入模板下载失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/stores/?repage";
    }

    /**
     * 落地配导出数据
     */
    @RequestMapping(value = "exportstores")
    public ModelAndView export(Stores stores, String ids, HttpServletRequest request, HttpServletResponse response) {

        List<Stores> storesList = new ArrayList<Stores>();

        if (StringUtils.isNotEmpty(ids)) {
            String idArray[] = ids.split(",");
            for (String id : idArray) {
                storesList.add(storesService.get(id));
            }
        } else {
            storesList = storesService.findList(stores);
        }

        try {
            Map<String, Object> dataMap = new HashMap<String, Object>();
            List<String> titles = new ArrayList<String>();
            //添加订单签收标题
            titles.add("公司名称");
            titles.add("仓库名称");
            titles.add("仓库区域");
            titles.add("仓库地址");
            titles.add("仓库负责人");
            titles.add("电话");
            titles.add("总面积(㎡)");
            titles.add("可租面积(㎡)");
            titles.add("仓库类型");
            titles.add("建筑类型");
            titles.add("单间长（M）");
            titles.add("单间宽（M）");
            titles.add("单间高（M）");
            titles.add("消防资质");
            titles.add("地坪处理");
            titles.add("价格");
            titles.add("价格单位");
            titles.add("仓库描述");
            titles.add("是否置顶");
            titles.add("置顶到期日期");
            titles.add("录入日期");
            titles.add("修改日期");
            dataMap.put("titles", titles);

            List<PageData> excelList = new ArrayList<PageData>();
            //添加表内容
            for (Stores yq : storesList) {
                PageData map = new PageData();
                map.put("var1", yq.getResource().getName());
                map.put("var2", yq.getName());
                if (StringUtils.isNotEmpty(yq.getStoreAres())) {
                    map.put("var3", yq.getStoreAres());
                } else {
                    map.put("var3", "");
                }
                if (StringUtils.isNotEmpty(yq.getAddress())) {
                    map.put("var4", yq.getAddress());
                } else {
                    map.put("var4", "");
                }
                if (StringUtils.isNotEmpty(yq.getContacts())) {
                    map.put("var5", yq.getContacts());
                } else {
                    map.put("var5", "");
                }
                if (StringUtils.isNotEmpty(yq.getTelephone())) {
                    map.put("var6", yq.getTelephone());
                } else {
                    map.put("var6", "");
                }
                if (StringUtils.isNotEmpty(yq.getTotalArea())) {
                    map.put("var7", yq.getTotalArea());
                } else {
                    map.put("var7", "");
                }
                if (StringUtils.isNotEmpty(yq.getAvailableArea())) {
                    map.put("var8", yq.getAvailableArea());
                } else {
                    map.put("var8", "");
                }
                if (StringUtils.isNotEmpty(yq.getStoresType())) {
                    if ("0".equals(yq.getStoresType())) {
                        map.put("var9", "普通");
                    }
                    if ("1".equals(yq.getStoresType())) {
                        map.put("var9", "恒温");
                    }
                    if ("2".equals(yq.getStoresType())) {
                        map.put("var9", "冷藏");
                    }
                    if ("3".equals(yq.getStoresType())) {
                        map.put("var9", "冷冻");
                    }
                    if ("4".equals(yq.getStoresType())) {
                        map.put("var9", "气调");
                    }
                    if ("5".equals(yq.getStoresType())) {
                        map.put("var9", "暖库");
                    }
                    if ("6".equals(yq.getStoresType())) {
                        map.put("var9", "危险");
                    }
                    if ("7".equals(yq.getStoresType())) {
                        map.put("var9", "保税");
                    }
                    if ("8".equals(yq.getStoresType())) {
                        map.put("var9", "海关监督");
                    }
                    if ("9".equals(yq.getStoresType())) {
                        map.put("var9", "医疗");
                    }
                    if ("10".equals(yq.getStoresType())) {
                        map.put("var9", "露天");
                    }
                    if ("11".equals(yq.getStoresType())) {
                        map.put("var9", "简易");
                    }
                } else {
                    map.put("var9", "");
                }
                if (StringUtils.isNotEmpty(yq.getArchitectureType())) {
                    if ("0".equals(yq.getArchitectureType())) {
                        map.put("var10", "平台");
                    }
                    if ("1".equals(yq.getArchitectureType())) {
                        map.put("var10", "高台");
                    }
                    if ("2".equals(yq.getArchitectureType())) {
                        map.put("var10", "平房");
                    }
                    if ("3".equals(yq.getArchitectureType())) {
                        map.put("var10", "楼房");
                    }
                    if ("4".equals(yq.getArchitectureType())) {
                        map.put("var10", "地下");
                    }
                    if ("5".equals(yq.getArchitectureType())) {
                        map.put("var10", "立体");
                    }
                    if ("6".equals(yq.getArchitectureType())) {
                        map.put("var10", "其他");
                    }
                } else {
                    map.put("var10", "");
                }
                if (StringUtils.isNotEmpty(yq.getMonomerAreaLength())) {
                    map.put("var11", yq.getMonomerAreaLength());
                } else {
                    map.put("var11", "");
                }
                if (StringUtils.isNotEmpty(yq.getMonomerAreaHigh())) {
                    map.put("var12", yq.getMonomerAreaHigh());
                } else {
                    map.put("var12", "");
                }
                if (StringUtils.isNotEmpty(yq.getMonomerAreaWide())) {
                    map.put("var13", yq.getMonomerAreaWide());
                } else {
                    map.put("var13", "");
                }
                if (StringUtils.isNotEmpty(yq.getFireQualification())) {
                    if ("0".equals(yq.getFireQualification())) {
                        map.put("var14", "有");
                    }
                    if ("1".equals(yq.getFireQualification())) {
                        map.put("var14", "没有");
                    }
                } else {
                    map.put("var14", "");
                }
                if (StringUtils.isNotEmpty(yq.getFloorTreatment())) {
                    String floorTreatment = "";
                    String str[] = yq.getFloorTreatment().split(",");
                    for (int i = 0; i < str.length; i++) {
                        if ("0".equals(str[i])) {
                            floorTreatment += "水泥,";
                        }
                        if ("1".equals(str[i])) {
                            floorTreatment += "防滑,";
                        }
                        if ("2".equals(str[i])) {
                            floorTreatment += "耐磨,";
                        }
                        if ("3".equals(str[i])) {
                            floorTreatment += "无尘,";
                        }
                    }
                    map.put("var15", floorTreatment);
                } else {
                    map.put("var15", "");
                }
                if ((yq.getPrice()) != null) {
                    map.put("var16", String.valueOf(yq.getPrice()));
                } else {
                    map.put("var16", "");
                }
                if (StringUtils.isNotEmpty(yq.getPriceUnit())) {
                    if ("0".equals(yq.getPriceUnit())) {
                        map.put("var17", "元/㎡/天");
                    }
                    if ("1".equals(yq.getPriceUnit())) {
                        map.put("var17", "元/㎡/月");
                    }
                } else {
                    map.put("var17", "");
                }
                if (StringUtils.isNotEmpty(yq.getStoresSummary())) {
                    map.put("var18", yq.getStoresSummary());
                } else {
                    map.put("var18", "");
                }
                if (StringUtils.isNotEmpty(yq.getTop())) {
                    if (yq.getTop().equals("0")) {
                        map.put("var19", "否");
                    }
                    if (yq.getTop().equals("1")) {
                        map.put("var19", "是");
                    }
                } else {
                    map.put("var19", "");
                }
                if ((yq.getTopDate()) != null) {
                    map.put("var20", DateUtils.formatDate(yq.getTopDate(), "yyyy-MM-dd HH:mm:ss "));
                } else {
                    map.put("var20", "");
                }
                if ((yq.getCreateDate()) != null) {
                    map.put("var21", DateUtils.formatDate(yq.getCreateDate(), "yyyy-MM-dd HH:mm:ss "));
                } else {
                    map.put("var21", "");
                }
                if ((yq.getUpdateDate()) != null) {
                    map.put("var22", DateUtils.formatDate(yq.getUpdateDate(), "yyyy-MM-dd HH:mm:ss "));
                } else {
                    map.put("var22", "");
                }
                excelList.add(map);

            }

            String fileName = "仓库" + DateUtils.formatDate(new Date(), "yyyy-MM-dd ");
            ;
            dataMap.put("fileName", fileName);

            dataMap.put("excelList", excelList);
            System.out.println("===导出excel成功！");
            ObjectExcelView erv = new ObjectExcelView();                    //执行excel操作

            return new ModelAndView(erv, dataMap);

        } catch (Exception e) {
            System.out.println("======导出excel时出错！" + e.getMessage());
        }
        return new ModelAndView("redirect:" + Global.getAdminPath() + "/tms/storesList");
    }

    /**
     * 导入Excel数据
     */
    @RequiresPermissions("tms:dropDispatch:import")
    @RequestMapping(value = "import", method = RequestMethod.POST)
    public String importFile(MultipartFile file, RedirectAttributes redirectAttributes) {
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//设置日期格式
        try {
            int successNum = 0;
            int failureNum = 0;
            StringBuilder failureMsg = new StringBuilder();
            ImportExcel ei = new ImportExcel(file, 1, 0);
            List<StoresImport> newList = ei.getDataList(StoresImport.class);
            for (StoresImport ddi : newList) {
                try {
                    Stores stores = new Stores();
                    if (StringUtils.isNotEmpty(ddi.getResource())) {
                        Resource resource = new Resource();
                        resource.setName(ddi.getResource());
                        List<Resource> resourseList = resourceService.findList(resource);
                        SortList<Resource> msLists = new SortList<Resource>();     //调用工具类，按照指定字段排序
                        msLists.sortByMethod(resourseList, "getCreateDate", false);
                        stores.setResource(resourseList.get(0));
                    }
                    if (StringUtils.isNotEmpty(ddi.getName())) {
                        stores.setName(ddi.getName());
                    }
                    if (StringUtils.isNotEmpty(ddi.getStoreAres())) {
                        stores.setStoreAres(ddi.getStoreAres());
                    }
                    if (StringUtils.isNotEmpty(ddi.getAddress())) {
                        stores.setAddress(ddi.getAddress());
                    }
                    if (StringUtils.isNotEmpty(ddi.getContacts())) {
                        stores.setContacts(ddi.getContacts());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTelephone())) {
                        stores.setTelephone(ddi.getTelephone());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTotalArea())) {
                        stores.setTotalArea(ddi.getTotalArea());
                    }
                    if (StringUtils.isNotEmpty(ddi.getAvailableArea())) {
                        stores.setAvailableArea(ddi.getAvailableArea());
                    }
                    if (StringUtils.isNotEmpty(ddi.getStoresType())) {
                        if ("普通".equals(ddi.getStoresType())) {
                            stores.setStoresType("0");
                        }
                        if ("恒温".equals(ddi.getStoresType())) {
                            stores.setStoresType("1");
                        }
                        if ("冷藏".equals(ddi.getStoresType())) {
                            stores.setStoresType("2");
                        }
                        if ("冷冻".equals(ddi.getStoresType())) {
                            stores.setStoresType("3");
                        }
                        if ("气调".equals(ddi.getStoresType())) {
                            stores.setStoresType("4");
                        }
                        if ("暖库".equals(ddi.getStoresType())) {
                            stores.setStoresType("5");
                        }
                        if ("危险".equals(ddi.getStoresType())) {
                            stores.setStoresType("6");
                        }
                        if ("保税".equals(ddi.getStoresType())) {
                            stores.setStoresType("7");
                        }
                        if ("海关监督".equals(ddi.getStoresType())) {
                            stores.setStoresType("8");
                        }
                        if ("医疗".equals(ddi.getStoresType())) {
                            stores.setStoresType("9");
                        }
                        if ("露天".equals(ddi.getStoresType())) {
                            stores.setStoresType("10");
                        }
                        if ("简易".equals(ddi.getStoresType())) {
                            stores.setStoresType("11");
                        }
                    }
                    if (StringUtils.isNotEmpty(ddi.getArchitectureType())) {
                        if ("平台".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("0");
                        }
                        if ("高台".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("1");
                        }
                        if ("平房".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("2");
                        }
                        if ("楼房".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("3");
                        }
                        if ("地下".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("4");
                        }
                        if ("立体".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("5");
                        }
                        if ("其他".equals(ddi.getArchitectureType())) {
                            stores.setArchitectureType("6");
                        }

                    }
                    if (StringUtils.isNotEmpty(ddi.getMonomerAreaLength())) {
                        stores.setMonomerAreaLength(ddi.getMonomerAreaLength());
                    }
                    if (StringUtils.isNotEmpty(ddi.getMonomerAreaHigh())) {
                        stores.setMonomerAreaHigh(ddi.getMonomerAreaHigh());
                    }
                    if (StringUtils.isNotEmpty(ddi.getMonomerAreaWide())) {
                        stores.setMonomerAreaWide(ddi.getMonomerAreaWide());
                    }
                    if (StringUtils.isNotEmpty(ddi.getFireQualification())) {
                        if ("有".equals(ddi.getFireQualification())) {
                            stores.setFireQualification("0");
                        }
                        if ("没有".equals(ddi.getFireQualification())) {
                            stores.setFireQualification("1");
                        }
                    }
                    if (StringUtils.isNotEmpty(ddi.getFloorTreatment())) {
                        String floorTreatment = "";
                        String str[] = ddi.getFloorTreatment().split(",");
                        for (int i = 0; i < str.length; i++) {
                            if ("水泥".equals(str[i])) {
                                floorTreatment += "0,";
                            }
                            if ("防滑".equals(str[i])) {
                                floorTreatment += "1,";
                            }
                            if ("耐磨".equals(str[i])) {
                                floorTreatment += "2,";
                            }
                            if ("无尘".equals(str[i])) {
                                floorTreatment += "3,";
                            }
                        }
                        stores.setFloorTreatment(floorTreatment);
                    }
                    if (StringUtils.isNotEmpty(ddi.getPrice())) {
                        stores.setPrice(Double.valueOf(ddi.getPrice()));
                    }
                    if (StringUtils.isNotEmpty(ddi.getPriceUnit())) {
                        if ("元/㎡/天".equals(ddi.getPriceUnit())) {
                            stores.setPriceUnit("0");
                        }
                        if ("元/㎡/月".equals(ddi.getPriceUnit())) {
                            stores.setPriceUnit("1");
                        }
                    }
                    if (StringUtils.isNotEmpty(ddi.getStoresSummary())) {
                        stores.setStoresSummary(ddi.getStoresSummary());
                    }
                    if (StringUtils.isNotEmpty(ddi.getTop())) {
                        if (ddi.getTop().equals("否")) {
                            stores.setTop("0");
                        } else if (ddi.getTop().equals("是")) {
                            stores.setTop("1");
                        } else {
                            stores.setTop("");
                        }

                    }
                    if ((ddi.getTopDate()) != null) {
                        stores.setTopDate(df.parse(ddi.getTopDate()));
                    }
                    storesService.save(stores);
                    successNum++;
                } catch (ConstraintViolationException ex) {
                    failureNum++;
                } catch (Exception ex) {
                    failureNum++;
                }
            }
            if (failureNum > 0) {
                failureMsg.insert(0, "，失败 " + failureNum + " 条订单记录。失败原因例如：导入数据出错！");
            }
            addMessage(redirectAttributes, "已成功导入 " + successNum + " 条订单记录" + failureMsg);
        } catch (Exception e) {
            addMessage(redirectAttributes, "导入订单失败！失败信息：" + e.getMessage());
        }
        return "redirect:" + Global.getAdminPath() + "/tms/stores/?repage";
    }
}